package com.cookor.poi.modules.cms.web;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.cms.entity.CmsQuestion;
import com.cookor.poi.modules.sys.utils.UserUtils;
import com.github.pagehelper.PageInfo;
import com.cookor.poi.modules.cms.entity.*;
import com.cookor.poi.modules.cms.service.QuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "${adminPath}/cms/question")
public class QuestionController extends BaseController {

    @Resource
    private QuestionService questionService;

    @ModelAttribute
    public CmsQuestion get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            questionService.updateHitsAddOne(id);
            return questionService.get(id);
        }else{
            return new CmsQuestion();
        }
    }


    @RequiresPermissions("cms:question:view")
    @RequestMapping(value = {"list", ""})
    public String list(CmsQuestion question, HttpServletRequest request, HttpServletResponse response, Model model) {
//		for (int i=0; i<10000000; i++){
//            CmsQuestion a = new CmsQuestion();
//			a.setTitle("测试测试测试测试测试测试测试测试" + i);
//			a.setQuestion(a.getTitle());
//			a.setCreateDate(new Date());
//            questionService.save(a);
//		}
        PageInfo<CmsQuestion> pageInfo = questionService.findPage(question, request.getParameter("pageNo"), request.getParameter("pageSize"));
        Page<CmsQuestion> page = new Page<CmsQuestion>();
        page.setPageNo(pageInfo.getPageNum());
        page.setCount(pageInfo.getPages());
        page.setPageSize(pageInfo.getPageSize());
        page.setList(pageInfo.getList());
        model.addAttribute("page", page);
        return "modules/cms/questionList";
    }

    @RequiresPermissions("cms:question:view")
    @RequestMapping(value = "form")
    public String form(CmsQuestion question, Model model) {
        // 如果当前传参有子节点，则选择取消传参选择
//        if (article.getCategory()!=null && StringUtils.isNotBlank(article.getCategory().getId())){
//            List<Category> list = categoryService.findByParentId(article.getCategory().getId(), Site.getCurrentSiteId());
//            if (list.size() > 0){
//                article.setCategory(null);
//            }else{
//                article.setCategory(categoryService.get(article.getCategory().getId()));
//            }
//        }
//        article.setArticleData(articleDataService.get(article.getId()));
//		if (article.getCategory()=null && StringUtils.isNotBlank(article.getCategory().getId())){
//			Category category = categoryService.get(article.getCategory().getId());
//		}
//        model.addAttribute("contentViewList",getTplContent());
//        model.addAttribute("article_DEFAULT_TEMPLATE",Article.DEFAULT_TEMPLATE);
        if(question != null && StringUtils.isNotBlank(question.getId())){
            question = questionService.get(question.getId());
        }else{
            question.setWeight(0);
        }
        model.addAttribute("question", question);
//        CmsUtils.addViewConfigAttribute(model, article.getCategory());
        return "modules/cms/questionForm";
    }
    @RequestMapping(value = "goto")
    public String goto2(Model model){
        return "modules/cms/questionIndexManage";
    }

    @RequiresPermissions("cms:article:edit")
    @RequestMapping(value = "save")
    public String save(CmsQuestion question, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, question)){
            return form(question, model);
        }
        questionService.save(question);
        addMessage(redirectAttributes, "保存常见提问'" + StringUtils.abbr(question.getTitle(),50) + "'成功");
        return "redirect:" + adminPath + "/cms/question/?"+("");
    }

    @RequiresPermissions("cms:article:edit")
    @RequestMapping(value = "delete")
    public String delete(CmsQuestion question, String categoryId, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
        // 如果没有审核权限，则不允许删除或发布。
        if (!UserUtils.getSubject().isPermitted("cms:article:audit")){
            addMessage(redirectAttributes, "你没有删除或发布权限");
        }
        questionService.delete(question);
        addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"文章成功");
        return "redirect:" + adminPath + "/cms/question/?"+("");
    }

}
